<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Contacts
 *
 * @author ADMIN
 */
class Contacts {

    private $_ID;
    private $_fullname;
    private $_email;
    private $_company;
    private $_address;
    private $_phone;
    private $_fax;
    private $_title;
    private $_content;
    private $_createdDate;
    private $_reply = 1;
    private $_replyContent;

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getFullname() {
        return $this->_fullname;
    }

    public function setFullname($fullname) {
        $this->_fullname = $fullname;
    }

    public function getEmail() {
        return $this->_email;
    }

    public function setEmail($email) {
        $this->_email = $email;
    }

    public function getCompany() {
        return $this->_company;
    }

    public function setCompany($company) {
        $this->_company = $company;
    }

    public function getAddress() {
        return $this->_address;
    }

    public function setAddress($address) {
        $this->_address = $address;
    }

    public function getPhone() {
        return $this->_phone;
    }

    public function setPhone($phone) {
        $this->_phone = $phone;
    }

    public function getFax() {
        return $this->_fax;
    }

    public function setFax($fax) {
        $this->_fax = $fax;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($title) {
        $this->_title = $title;
    }

    public function getContent() {
        return $this->_content;
    }

    public function setContent($content) {
        $this->_content = $content;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function setCreatedDate($createdDate) {
        $this->_createdDate = $createdDate;
    }

    public function getReply() {
        return $this->_reply;
    }

    public function setReply($reply) {
        $this->_reply = $reply;
    }

    public function getReplyContent() {
        return $this->_replyContent;
    }

    public function setReplyContent($replyContent) {
        $this->_replyContent = $replyContent;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `contacts`
                                (
                                 `CT_Fullname`,
                                 `CT_Email`,
                                 `CT_Company`,
                                 `CT_Address`,
                                 `CT_Phone`,
                                 `CT_Fax`,
                                 `CT_Title`,
                                 `CT_Content`,
                                 `CT_CreatedDate`,
                                 `CT_Reply`,
                                 `CT_ReplyContent`)
                    VALUES (
                            :CT_Fullname,
                            :CT_Email,
                            :CT_Company,
                            :CT_Address,
                            :CT_Phone,
                            :CT_Fax,
                            :CT_Title,
                            :CT_Content,
                            :CT_CreatedDate,
                            :CT_Reply,
                            :CT_ReplyContent);";

            $parameters = array(
                ':CT_Fullname', ':CT_Email',
                ':CT_Company', ':CT_Address',
                ':CT_Phone', ':CT_Fax',
                ':CT_Title', ':CT_Content',
                ':CT_CreatedDate', ':CT_Reply',
                ':CT_ReplyContent'
            );

            $values = array(
                $this->getFullname(), $this->getEmail(),
                $this->getCompany(), $this->getAddress(),
                $this->getPhone(), $this->getFax(),
                $this->getTitle(), $this->getContent(),
                $this->getCreatedDate(), $this->getReply(),
                $this->getReplyContent()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_STR
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db = null) {

        if ($db != null) {
            $sql = "DELETE
                    FROM `contacts`
                    WHERE `CT_ID` = :CT_ID;";

            $parameters = array(':CT_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function getList($db = null) {
        if ($db != null) {

            $sql = 'SELECT
                        `CT_ID`,
                        `CT_Fullname`,
                        `CT_Email`,
                        `CT_Company`,
                        `CT_Address`,
                        `CT_Phone`,
                        `CT_Fax`,
                        `CT_Title`,
                        `CT_Content`,
                        `CT_CreatedDate`,
                        `CT_Reply`,
                        `CT_ReplyContent`
                      FROM `contacts`
                      ORDER BY CT_CreatedDate DESC';

            $parameters = null;
            $values = null;
            $types = null;

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $contact = new Contacts();
                    $contact->setID($row->CT_ID);
                    $contact->setFullname($row->CT_Fullname);
                    $contact->setEmail($row->CT_Email);
                    $contact->setCompany($row->CT_Company);
                    $contact->setAddress($row->CT_Address);
                    $contact->setPhone($row->CT_Phone);
                    $contact->setFax($row->CT_Fax);
                    $contact->setTitle($row->CT_Title);
                    $contact->setContent($row->CT_Content);
                    $contact->setCreatedDate($row->CT_CreatedDate);
                    $contact->setReply($row->CT_Reply);
                    $contact->setReplyContent($row->CT_ReplyContent);

                    $rows[] = $contact;
                }
                return $rows;
            } else {
                return null;
            }
        } else {
            echo 'Connection is null';
        }
    }

}

?>
